import { createApp } from 'vue'
import { i18n } from '@/lang'
import AlertComponent from './common-confirm.vue'

let dialog = undefined

const Confirm = (options = {}) => {
  const opts = {
    title: '',
    loading: false,
    ...options,
    cancelText: i18n.t('common.common_confirm.button.cancel')
  }

  if (dialog) {
    return dialog
  }

  const parent = document.body

  const instance = createApp(AlertComponent, {
    el: document.createElement('div'),
    data: opts
  })

  parent.appendChild(instance.$el)

  Vue.nextTick(() => {
    instance.dialogVisible = true
  })

  instance.close = () => {
    instance.dialogVisible = false
    parent.removeChild(instance.$el)
    dialog = undefined
  }

  dialog = instance

  return instance
}

export default Confirm
